package com.pengzy.core.security;

import com.pengzy.comment.core.utils.JsonUtils;
import com.pengzy.comment.web.result.JsonResult;
import com.pengzy.comment.web.result.ServletUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** AccessDeniedHandler： 授权异常失败处理
 * @author xiaozhi
 */
@Component
public class AccessDeniedHandlerFilter implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ServletUtils.renderString(response, JsonUtils.toJsonString(JsonResult.error(403,"没有权限!请联系管理员")));
    }
}
